package org.jet.emall.prod.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jet.emall.common.constant.OrderStatusValue;
import org.jet.emall.common.util.OrderCodeFactory;
import org.jet.emall.common.util.SnowflakeIdWorker;
import org.jet.emall.prod.dto.NewOrderDTO;
import org.jet.emall.prod.dto.OrderDTO;
import org.jet.emall.prod.dto.OrderItemDetailViewDTO;
import org.jet.emall.prod.po.OrderItemDetail;
import org.jet.emall.prod.po.OrderItemDetailView;
import org.jet.emall.prod.po.OrderMaster;
import org.jet.emall.prod.po.ProdSku;
import org.jet.emall.prod.service.mp.IOrderItemDetailService;
import org.jet.emall.prod.service.mp.IOrderItemDetailViewService;
import org.jet.emall.prod.service.mp.IOrderMasterService;
import org.jet.emall.prod.service.mp.IProdSkuService;
import org.jet.emall.prod.transfer.OrderDtoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-30 11:37
 */

@Service
public class CustomOrderServiceImpl implements CustomOrderService {

    @Autowired
    private IOrderMasterService orderMasterService;

    @Autowired
    private IOrderItemDetailService orderItemDetailService;

    @Autowired
    private IProdSkuService prodSkuService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private IOrderItemDetailViewService orderItemDetailViewService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long save(NewOrderDTO dto) {

        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setAddress(dto.getAddress())
                .setCity(dto.getCity())
                .setProvince(dto.getProvince())
                .setCustomerId(dto.getCustomerId())
                .setDistrict(dto.getDistrict())
                .setShippingUser(dto.getShippingUser())
                .setShippingPhone(dto.getShippingPhone())
                .setShippingMoney(dto.getShippingMoney())
                .setOrderMoney(dto.getOrderMoney())
                .setStatus(OrderStatusValue.UNPAID.getValue())
                .setCode(OrderCodeFactory.getOrderCode(dto.getCustomerId()))
                .setId(snowflakeIdWorker.nextId());
        orderMasterService.save(orderMaster);

        List<OrderItemDetail> items = new LinkedList<>();
        dto.getItems().forEach(item->{
            ProdSku sku = prodSkuService.getById(item.getSkuId());

            OrderItemDetail detail = new OrderItemDetail();
            detail.setOrderId(orderMaster.getId())
                    .setCount(item.getCount())
                    .setSkuId(item.getSkuId())
                    .setAverageCost(sku.getCost())
                    .setPrice(sku.getPrice())
                    .setId(snowflakeIdWorker.nextId())
                    .setWarehouseId(1);
            items.add(detail);
        });
        orderItemDetailService.saveBatch(items);

        return orderMaster.getId();
    }

    @Override
    public List<OrderDTO> list(Long customerId) {
        List<OrderMaster> list = orderMasterService.list(new QueryWrapper<OrderMaster>().eq("customer_id", customerId));
        List<OrderDTO> dtoList = OrderDtoTransfer.INSTANCE.toDtoList(list);
        dtoList.forEach(orderDTO -> {
            List<OrderItemDetailView> views = orderItemDetailViewService.list(new QueryWrapper<OrderItemDetailView>().eq("order_id", orderDTO.getId()));
            List<OrderItemDetailViewDTO> dtos =  OrderDtoTransfer.INSTANCE.toItemDtoList(views);
            orderDTO.setItems(dtos);
        });
        return dtoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(String code, Byte status) {
        orderMasterService.update(new UpdateWrapper<OrderMaster>().eq("code",code).set("status",status));
    }


    @Override
    public OrderDTO getOrderById(Long id) {
        OrderMaster orderMaster = orderMasterService.getById(id);
        OrderDTO orderDTO = OrderDtoTransfer.INSTANCE.toDto(orderMaster);
        List<OrderItemDetailView> itemDetailViews = orderItemDetailViewService.list(new QueryWrapper<OrderItemDetailView>().eq("order_id", orderDTO.getId()));
        orderDTO.setItems(OrderDtoTransfer.INSTANCE.toItemDtoList(itemDetailViews));
        return orderDTO;
    }


}
